<?php
header('Content-type:text/html;charset=UTF-8');
require "Classes/PHPExcel.php";//引入PHPExcel加载文件


class ReqDataType{
	public $status='error';
	public $type='procFile';
//	public $filename='';
	public $tableJson;
	public $msg;
}


class UploadDataType{
	public $userid='';
	public $tableid='';
	public $filename='';
	public $rows=[];
	public $json='';
	public $len=0;
	
	//进行 json 数据化
	function getJson(){
		$tableJson = new  TableJsonType();
		$tableJson->tableid=$this->tableid;
		$tableJson->rowlist=$this->rows;
		$tableJson->len=$this->len;
		$this->json=json_encode($tableJson);
//		echo $this->json;
		return json_encode($tableJson);
	}
	
	function getSqlinTablelist(){
		$sql = "INSERT INTO `tablelist` ( `userid`, `filename`) VALUES ( '$this->userid', '$this->filename')";
		return $sql;
	}
	

}

class TableJsonType{
	public $tableid="";
	public $rowlist=[];
	public $len=0;
}

class FileProc{
	public $timeNull=0;
	public $times=0;
	public $len=0;
	public $data;
	public $dataBody;
	public $uploadDataMsg;
	public $arr=array();
	
	function setUpLoadDataMsg($uploadDataType){
		$this->$uploadDataType;
	}
	
	function getUpLoadDataMsg(){
		return $this->$uploadDataType;
	}
	// 获取 表格 的数据
	function getFileMsg($filename){
		$obj_PHPExcel = PHPExcel_IOFactory::load("temp/".$filename); //加载文件
		$sheet_count = $obj_PHPExcel->getSheetCount(); //获取文件sheet数量
//		$this->data=array();
		/*1.全部读取*/
		for ($i=0; $i < $sheet_count; $i++) {
		    $this->data = $obj_PHPExcel->getSheet($i)->toArray();//读取每个sheet的数据放入数组中
			//$sql=sql_list("teble",$data[0]);
			//echo $sql;
			
			return ($this->data);
		}
//		print_r($this->data);
//		return $this->data;

	}	
	//数据json 化 
	function getJsonDataHeader($tableJsonType){
		return json_encode($tableJsonType);
	}
	
	//提取 表的 第一行头
	function procHeader($dataarray){
//		print_r($this->data);
		$cursor=0;
		foreach($dataarray[0] as $key=>$val){
			$this->times++;
//			$cursor++;
			if(empty($val)||$val==null||$val=="Null"){
				if($this->timeNull<1)$cursor=$this->times;
				$this->timeNull++;
				$this->arr[]="Null".$this->times;
				//if($this->timeNull>2)
				if($this->timeNull>3){
				break;
			}
				continue;
				
			}
			
			$this->arr[]=$val;
//			if($this->times-$cursor>$this->timeNull){
//				echo "ok";
//			}
			if($this->times-$cursor>=3)$this->timeNull=0;
//			echo $this->times ." ".$cursor." ".$this->timeNull ."</br>";
//			$this->timeNull=0;
		}
		
		
//		$this->len=$this->times-$this->timeNull;
		
//		echo $this->len;
//如果 最后一个 是 空格 则 剔除
//echo $this->arr[$this->len];
//print_r($this->arr);
//		if(strpos($this->arr[count($this->arr)-1], "Null")===true )
//		echo "dd";
//		else echo "daa";
		if( strpos($this->arr[count($this->arr)-1], "Null")!==false||$this->arr[count($this->arr)-1]==null){
//			unset($this->arr[count($this->arr)]);
			//$this->len--;
//			echo $this->len;
			array_pop($this->arr);
//			print_r($this->arr);
		}
		if( strpos($this->arr[count($this->arr)-1], "Null")!==false||$this->arr[count($this->arr)-1]==null){
//			unset($this->arr[count($this->arr)]);
			//$this->len--;
//			echo $this->len;
			array_pop($this->arr);
//			print_r($this->arr);
		}
		if( strpos($this->arr[count($this->arr)-1], "Null")!==false||$this->arr[count($this->arr)-1]==null){
//			unset($this->arr[count($this->arr)]);
			//$this->len--;
//			echo $this->len;
			array_pop($this->arr);
//			print_r($this->arr);
		}
		
		if( strpos($this->arr[count($this->arr)-1], "Null")!==false||$this->arr[count($this->arr)-1]==null){
//			unset($this->arr[count($this->arr)]);
			//$this->len--;
//			echo $this->len;
			array_pop($this->arr);
//			print_r($this->arr);
		}

//		$this->len--;
		$this->len=count($this->arr);
//		echo "END:".count($this->arr)."OK";
//		print_r($this->arr);
//		echo "LEN : ".$this->len."OK";
//		echo "</br></br></br></br></br>";
		return $this->arr;
	}
	
	function getSqlCreate($uploadDataType){
		$headernum=$uploadDataType->len;
		$sql="CREATE TABLE `I_tebles`.`$uploadDataType->tableid` ";
		$sql_row="( ";
		foreach($uploadDataType->rows as $row ){
			$sql_row=$sql_row."`$row` VARCHAR(60) NULL";
			if($headernum-->1){
				$sql_row=$sql_row.",";
			//	echo $headernum;
			}
			//echo $headernum;
		}
		$sql_row=$sql_row." ) ";
		$sql=$sql.$sql_row."ENGINE = InnoDB;";
		//echo $sql;
		return $sql;
	}
	//获取data 
	function getData(){
//		print_r($this->data);
		return $this->data;
	}
	//测试 数据
	function showData(){
		echo $this->timeNull;
		echo $this->times;
		echo $this->len;
		print_r($this->arr);
	}
	//将  组装数据 
 	function packUploadDataJson($dataall){
 		$AllData=array();
		foreach($dataall as $rows){
			$row=[];
			for($i = 0 ;$i<$this->len;$i++){
				$row[]=$rows[$i];
			}
			$AllData[]=$row;
		}
		unset($AllData[0]);
		$this->dataBody=$AllData;
//		unset($this->dataBody[0]);
//		print_r()
//		print_r($AllData);
	}
	
	function getBodySqlValue($uploadDataType){
		$sqls="";
		$sql_var="INSERT INTO  `I_tebles`.`".$uploadDataType->tableid."` ".$this->getTableRows($uploadDataType->rows,$uploadDataType->len)." ";
		foreach($this->dataBody as $rows){
			$sqls =$sqls . $sql_var ." VALUES ".$this->getTableValues($rows,$this->len)." ;";
		}
		return $sqls;
	}
	
	function getTableRows($rows,$len){
		$sql=" ( ";
		
		for($i=0;$i<$len;$i++){
			$sql=$sql."`".$rows[$i]."` ";
			if($i<$len-1)
			{
				$sql=$sql.", ";
			}
		}
		
		return $sql." ) ";
	}
	function getTableValues($rows,$len){
	$sql=" ( ";
	
	for($i=0;$i<$len;$i++){
		$sql=$sql."'".$rows[$i]."'";
		if($i<$len-1)
		{
			$sql=$sql.", ";
		}
	}
	
	return $sql." ) ";
	}
}
//
//$dataarray=FileProc::getFileMsg("./temp/1234.xls");
//$times=0;
//$timeNull=0;
//$arr=array();
//foreach($dataarray[0] as $key=>$val){
//	$times++;
//	
//	if(empty($val)||$val==null){
//		$timeNull++;
//		continue;
//	}
//	
//	if($timeNull>3){
//		break;
//	}
//	$arr[]=$val;
//	$timeNull=0;
//}
//print_r($arr);
//echo $times."aaa".$timeNull;
////
//$uploadDataMsg=new UploadDataType();
//
//$fileProc= new FileProc();
////
////$data = $fileproc->getFileMsg("1234.xls");
//$fileProc->getFileMsg("1234.xls");
//	//获取 表格的 头信息
//$headData=$fileProc->procHeader($fileProc->data);
//
//
//$uploadDataMsg->rows=$headData;
//$uploadDataMsg->len=$fileProc->len;
//$uploadDataMsg->tableid="user_1_table_1";
//
//$fileProc->packUploadDataJson($fileProc->data);
////print_r($uploadDataMsg);
//echo $fileProc->getBodySqlValue($uploadDataMsg);
////$fileproc->procHeader($data);
////print_r($fileproc->getData());
//$fileproc->showData();
//print_r($fileProc->dataBody);
?>